package com.zhoug.common3.utils;


import android.os.Handler;
import android.os.Looper;

import com.zhoug.common3.interfaces.DefaultLifecycleObserver;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

/**
 * 显示当前时间
 * 每秒刷新
 * @Author 35574
 * @Date 2021/7/23
 * @Description
 */
public class DateTimeTimer {
    private static final String TAG = ">>>DateTimeTimer";
    private static  boolean debug=false;

    private static final String DEF_PATENT_DATA_TIME = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期格式化模式
     */
    private String pattern = DEF_PATENT_DATA_TIME;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(pattern, Locale.CANADA);
    private Date mDate = new Date();
    /**
     * 回掉
     */
    private OnTimeListener mOnTimeListener;

    public DateTimeTimer(LifecycleOwner owner) {
        if (owner != null) {
            owner.getLifecycle().addObserver( new DefaultLifecycleObserver() {
                @Override
                public void onResume(@NonNull LifecycleOwner owner) {
                    start();
                }

                @Override
                public void onPause(@NonNull LifecycleOwner owner) {
                    stop();
                }
            });
        }
    }


    private Runnable mDateRunnable = new Runnable() {
        @Override
        public void run() {
            if (mOnTimeListener != null) {
                mDate.setTime(System.currentTimeMillis());
                String formatDate = mSimpleDateFormat.format(mDate);
                if (debug) {
                    LogUtils.d(TAG, "formatDate:"+formatDate);
                }
                mOnTimeListener.onResult(formatDate);
            }
            mHandler.postDelayed(mDateRunnable, 1000);
        }
    };

    /**
     * 开启
     */
    public synchronized void start() {
        if (debug) {
            LogUtils.d(TAG, "start:");
        }
        mHandler.removeCallbacks(mDateRunnable);
        mHandler.post(mDateRunnable);
    }

    /**
     * 停止
     */
    public synchronized void stop() {
        if (debug) {
            LogUtils.d(TAG, "stop:");
        }
        mHandler.removeCallbacks(mDateRunnable);
    }


    /**
     * 日期格式化模式,默认{@link #DEF_PATENT_DATA_TIME}
     *
     * @param pattern
     * @return
     */
    public DateTimeTimer setPattern(String pattern) {
        if (pattern != null) {
            this.pattern = pattern;
            mSimpleDateFormat = new SimpleDateFormat(this.pattern, Locale.CANADA);
        }
        return this;
    }


    /**
     * 设置时间改变监听器
     *
     * @param onTimeListener
     * @return
     */
    public DateTimeTimer setOnTimeListener(OnTimeListener onTimeListener) {
        this.mOnTimeListener = onTimeListener;
        return this;
    }


    public static void setDebug(boolean debug) {
        DateTimeTimer.debug = debug;
    }

    /**
     * 间改变监听器
     */
    public interface OnTimeListener {
        void onResult(String formatDate);
    }
}
